﻿using System.Collections.Generic;
using System.Linq;
using compshop.Infrastructure;
using compshop.Infrastructure.CompShopDataService;

namespace compshop.Modules.GeneralProductParameters.Presenter
{
    public class GeneralProductParametersPresenter
    {
        private readonly CompShopEntities contextDS;
        private IList<GeneralProductParameter> parameters;
        private EventsManager eventsManager;

        public GeneralProduct SelectedGeneralProduct { get; set; }

        public GeneralProductParametersPresenter(EventsManager eventsManager)
        {
            contextDS = CompShopEntitiesContext.GetInstance();
            this.eventsManager = eventsManager;
            eventsManager.Presenter = this;
            eventsManager.Subscribe();

            parameters = new List<GeneralProductParameter>();         
        }

        public IViewGeneralProductParameters View { get; set; }

        public IList<GeneralProductParameter> Parameters
        {
            get { return parameters; }
            set { parameters = value; }
        }

        #region Publishers
        #endregion

        #region Handlers

        public void GeneralProductSelectedEventHandler(int id)
        {
            if (id != 0)
            {
                parameters = contextDS.GeneralProductParameters.Expand("Parameter,GeneralProduct").
                        Where(x => x.GeneralProductID == id).ToList<GeneralProductParameter>();
                SelectedGeneralProduct = contextDS.GeneralProducts.Expand("SubCategory,Producer")
                    .Where(x => x.GeneralProductID == id).First();
                View.Presenter = this;
            }
            else
                View.Clear();
        }

        #endregion

    }
}





//parameters = contextDS.GeneralProductParameters.Expand("Parameter, GeneralProduct").
//        Where(x => x.GeneralProductID == 1).ToList<GeneralProductParameter>();

//var query = from pr in contextDS.GeneralProductParameters
//            join p in contextDS.Parameters
//            on pr.ParameterID equals p.ParameterID
//            where (pr.GeneralProductID == 1)
// select p;
